<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    div{
        width: 400px;
        height: 400px;
        background: violet;
        margin: auto;
    }
</style>
<body>
    <!-- 封装一个十六进制的随机颜色 函数 -->
    <div id="demo">
    </div>
    <script>
        //获取元素
        document.getElementById("demo").style.background = yanSe();
        function yanSe(){
            var str = "#"
            var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
            for (var i = 0; i < 6; i++) {
                //产生的每个随机数都是一个索引,根据索引找到数组中对应的值,拼接到一起
                str += arr[parseInt(Math.random() * 16)];
            }
            return str;
        }
        
    </script>
</body>
</html>